package praca.framework;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import praca.framework.GameGL.GameState;
import praca.gra.StartScreen;
import praca.interfejsy.Texture;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;

public class GameRenderer implements Renderer{

	final float CO_SEKUNDE = 1 * 1000000000;
	final float FPS30 = 1000000000/30;
	final float FPS25 = 1000000000/25;
	final float FPS60 = 1000000000/60;
	int updatenr = 0;
	GameGL game;
	GL10 gl;
	float startTime;
	float deltaTime;
	
	public GameRenderer(GameGL gra){
		game = gra;
	}
	
	@Override
	public void onDrawFrame(GL10 gl) {

		float deltaTime = (System.nanoTime()-startTime) / 1000000000.0f;
        startTime = System.nanoTime();
        
        game.currentScreen.update(deltaTime);
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		Log.d("kolejnosc", "OnSurfaceChanged!!!");
		
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		Log.d("kolejnosc", "OnSurfaceCreated");
		this.gl = gl;
		game.getGraphics().setGL(gl);
		
		//jakis bug
		if(game.getView().getWidth() > game.getView().getHeight()){
			gl.glViewport(0, 0, game.getView().getWidth(), game.getView().getHeight());

			game.width = game.getView().getWidth();
			game.height =  game.getView().getHeight();
			
			
		}else{
			gl.glViewport(0, 0, game.getView().getHeight(), game.getView().getWidth());
			game.width = game.getView().getHeight();
			game.height =  game.getView().getWidth();
			
		}
		
		
			

		
		//Log.d("touch", "Width: "+game.getView().getWidth()+" ,Height: "+game.getView().getHeight());
		if(game.gameState == GameState.INITIALISATION)
		game.getInput().setScreenSize(game.getView().getWidth(), game.getView().getHeight());
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glOrthof(0, 800, 0, 480, 1, -1);
		gl.glEnable(GL10.GL_BLEND);
		if(game.gameState == GameState.INITIALISATION){
			game.currentScreen = game.getStartScreen();
		}
		game.gameState = GameState.RUNNING;	
		game.currentScreen.resume();
		startTime = System.nanoTime();
	}
	
	public GL10 getGL10(){
		return gl;
	}

}
